package com.lsh.oms.provider.common.rest;

import com.lsh.base.common.model.CommonResult;
import com.lsh.oms.core.exception.BusinessException;
import org.apache.log4j.Logger;

import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;

/**
 * Created by huangdong on 16/7/15.
 */
public class BusinessExceptionMapper implements ExceptionMapper<BusinessException> {

    private static final Logger logger = Logger.getLogger(BusinessExceptionMapper.class);

    @Override
    public Response toResponse(BusinessException e) {
        logger.error(e.getMessage(), e);
        return this.handleBusinessException(e);
    }

    protected Response handleBusinessException(BusinessException e) {
        return Response.status(Response.Status.BAD_REQUEST).entity(new CommonResult<Integer>(e.getCode(), e.getMessage())).build();
    }
}
